<?php
require_once(ROOTDIR."classes/db_mysql.php");

class BaseDB {
	var $_db_reader = NULL;
	var $_db_writer = NULL;
	
	function BaseDB() {
		$this->_db_reader = $GLOBALS['db_reader'];
		$this->_db_writer = $GLOBALS['db_writer'];
	}

	private function _getReaderConnection() {
		//global
		return $GLOBALS['db_reader'];
	}
	
	private function _getWriterConnection(){
		return $GLOBALS['db_writer'];
	}

	public function _getUpdateSqlString($arr) {
		//global
		return $this->sqlSingle($arr);
	}

	public function _getAllResultFromQuery($query) {
		$result = array ();
		$i=0;
		while ( $rt = $this->_db_reader->fetch_array ( $query ) ) {
			foreach($rt as $key=>$val){
				//if(is_numeric($val)) {$result[$i][$key] = $val;}
				//else if(is_string($val)) {$result[$i][$key] = iconv("UTF-8",'GBK',$val);}
				//else {$result[$i][$key] = $val;}
				$result[$i][$key] = $val;
			}
			$i++;			
		}
		return $result;
	}

	public function _checkAllowField($fieldData,$allowFields) {
		foreach ($fieldData as $key=>$value) {
			if (!in_array($key,$allowFields)) {
				unset($fieldData[$key]);
			}
		}
		return $fieldData;
	}

	public function _addSlashes($var) {
		//global
		return $this->sqlEscape($var);
	}

	public function _getImplodeString($string,$strip=true) {
		return arrayImplode($string,$strip);
	}

	public function _serialize($value) {
		if (!is_string($value) && !is_numeric($value)) {
			$value = serialize($value);
		}
		return $value;
	}

	public function _unserialize($value) {
		if ($value && is_array($tmpValue = unserialize($value))) {
			$value = $tmpValue;
		}
		return $value;
	}
	
	/**
	 * 针对SQL语句的变量进行反斜线过滤,并两边添加单引号
	 *
	 * @param mixed $var 过滤前变量
	 * @param boolean $strip 数据是否经过stripslashes处理
	 * @return mixed 过滤后变量
	 */
	public function sqlEscape($var,$strip = true) {
		if (is_array($var)) {
			foreach ($var as $key => $value) {
				$var[$key] = trim($this->sqlEscape($value,$strip));
			}
			return $var;
		} elseif (is_numeric($var)) {
			return " '".$var."' ";
		} else {
			$var =  iconv("GBK", "UTF-8", $var);
			return " '".addslashes($strip ? stripslashes($var) : $var)."' ";
		}
	}
	
	/**
	 * 构造单记录数据更新SQL语句
	 *  格式: field='value',field='value'
	 *
	 * @param array $array 更新的数据,格式: array(field1=>'value1',field2=>'value2',field3=>'value3')
	 * @param boolean $strip 数据是否经过stripslashes处理
	 * @return string SQL语句
	 */
	public function sqlSingle($array,$strip=true) {
		$array = $this->sqlEscape($array,$strip);
		$str = '';
		foreach ($array as $key => $val) {
			$str .= ($str ? ', ' : ' ').$key.'='.$val;
		}
		return $str;
	}
	/**
	 * 构造批量数据更新SQL语句
	 *  格式: ('value1[1]','value1[2]','value1[3]'),('value2[1]','value2[2]','value2[3]')
	 *
	 * @param array $array 更新的数据,格式: array(array(value1[1],value1[2],value1[3]),array(value2[1],value2[2],value2[3]))
	 * @param boolean $strip 数据是否经过stripslashes处理
	 * @return string SQL语句
	 */
	public function sqlMulti($array,$strip=true) {
		$str = '';
		foreach ($array as $val) {
			if (!empty($val)) {
				$str .= ($str ? ', ' : ' ') . '(' . arrayImplode($val,$strip) .') ';
			}
		}
		return $str;
	}
	/**
	 * SQL查询中,构造LIMIT语句
	 *
	 * @param integer $start 开始记录位置
	 * @param integer $num 读取记录数目
	 * @return string SQL语句
	 */
	public function sqlLimit($start,$num=false) {
		return ' LIMIT '.($start <= 0 ? 0 : (int)$start).($num ? ','.abs($num) : '');
	}	
}

?>
